package rtest;

import java.io.IOException;

import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;

public class DemoRJava {
	/*
	 * JGR Rserve JRI
	 * http://blog.csdn.net/faith_mo_blog/article/details/45337389 install R:
	 * https://projects.uabgrid.uab.edu/r-group/wiki/install-R ./configure
	 * --with-readline=no --with-x=no
	 */
	public static void main(String[] args) throws IOException {
		Rengine engine = new Rengine(null, false, null);

		String fileName = DemoRJava.class.getClassLoader().getResource("bpara.R").getFile();
		engine.eval("source('" + fileName.substring(1) + "', encoding = 'UTF-8')");

		REXP rexp;
		int[] a = { 1, 23, 3 };
		int[] b = { 1, 23, 23 };
		engine.assign("a", a);
		engine.assign("b", b);

		rexp = engine.eval("bpara(a,b)");
		System.out.println(rexp.asDouble());

		// test(engine);
	}

	private static void test(Rengine engine) {
		int[] arr = new int[] { -1, 2, 1, -3, 5, 4, -2 };
		engine.assign("x", arr);
		REXP rexp = engine.eval("max(x)");
		int max = rexp.asInt();
		System.out.println(max);
		engine.end();
	}
}
